using Circus.CodeEditor;
using Circus.CodeEditor.TextSource;
using Circus.Syntax;
using Circus.Syntax.Lexer;
using System;
using System.Drawing;
using System.IO;
namespace Circus.CodeEditor.Serialization
{
	public class FmtExport  
	{
		protected Color foreColor = Consts.DefaultControlForeColor;
		protected Color backColor = Consts.DefaultControlBackColor;
		protected FontStyle fontStyle = FontStyle.Regular;
		protected bool firstLine = true;
		protected TextWriter writer;
		protected CodeEditor edit;
		protected virtual void WriteForeColor(Color foreColor)
		{
		}
		protected virtual void WriteBackColor(Color backColor)
		{
		}
		protected virtual void RemoveFontStyle(FontStyle style)
		{
		}
		protected virtual void AddFontStyle(FontStyle style)
		{
		}
		protected virtual void StartLine()
		{
		}
		protected virtual void EndLine()
		{
		}
		protected virtual void WriteText(int pos, string text)
		{
		}
		protected virtual void WriteFontStyle(FontStyle style)
		{
			FontStyle fontStyle = this.fontStyle & ~style;
			FontStyle fontStyle2 = style & ~this.fontStyle;
			if ((fontStyle & FontStyle.Strikeout) != FontStyle.Regular)
			{
				this.RemoveFontStyle(FontStyle.Strikeout);
			}
			if ((fontStyle & FontStyle.Underline) != FontStyle.Regular)
			{
				this.RemoveFontStyle(FontStyle.Underline);
			}
			if ((fontStyle & FontStyle.Italic) != FontStyle.Regular)
			{
				this.RemoveFontStyle(FontStyle.Italic);
			}
			if ((fontStyle & FontStyle.Bold) != FontStyle.Regular)
			{
				this.RemoveFontStyle(FontStyle.Bold);
			}
			if ((fontStyle2 & FontStyle.Bold) != FontStyle.Regular)
			{
				this.AddFontStyle(FontStyle.Bold);
			}
			if ((fontStyle2 & FontStyle.Italic) != FontStyle.Regular)
			{
				this.AddFontStyle(FontStyle.Italic);
			}
			if ((fontStyle2 & FontStyle.Underline) != FontStyle.Regular)
			{
				this.AddFontStyle(FontStyle.Underline);
			}
			if ((fontStyle2 & FontStyle.Strikeout) != FontStyle.Regular)
			{
				this.AddFontStyle(FontStyle.Strikeout);
			}
		}
		protected virtual void ApplyStyle(FontStyle style, Color foreColor, Color backColor)
		{
			if (foreColor != this.foreColor && foreColor != Color.Empty)
			{
				this.WriteForeColor(foreColor);
				this.foreColor = foreColor;
			}
			if (backColor != this.backColor && backColor != Color.Empty)
			{
				this.WriteBackColor(backColor);
				this.backColor = backColor;
			}
			if (style != this.fontStyle)
			{
				this.WriteFontStyle(style);
				this.fontStyle = style;
			}
		}
		protected virtual void WriteLine(EditSyntaxPaint syntaxPaint, string line, StringItemInfo style, int start, int end)
		{
			TextStyle textStyle = TextStyle.None;
			LexStyle lexStyle = (syntaxPaint != null) ? syntaxPaint.GetLexStyle(style, ref textStyle) : null;
			if (lexStyle != null)
			{
				this.ApplyStyle(syntaxPaint.GetFontStyle(lexStyle.FontStyle, textStyle), (lexStyle.ForeColor != Color.Empty) ? syntaxPaint.GetFontColor(lexStyle.ForeColor, textStyle) : syntaxPaint.GetForeColor(false), (lexStyle.BackColor != Color.Empty) ? lexStyle.BackColor : syntaxPaint.GetBackColor(false));
			}
			else
			{
				if (syntaxPaint != null)
				{
					this.ApplyStyle(syntaxPaint.GetFontStyle(FontStyle.Regular, textStyle), syntaxPaint.GetFontColor(syntaxPaint.GetForeColor(false), textStyle), syntaxPaint.GetBackColor(false));
				}
			}
			this.WriteText(start, line.Substring(start, end - start + 1));
		}
		public virtual void WriteLine(StringItem item)
		{
			int length = item.String.Length;
			this.StartLine();
			this.firstLine = false;
			EditSyntaxPaint editSyntaxPaint = (this.edit != null) ? this.edit.SyntaxPaint : null;
			if (editSyntaxPaint != null && item.String != string.Empty)
			{
				StringItemInfo stringItemInfo = item.TextData[0];
				StringItemInfo stringItemInfo2 = stringItemInfo;
				int num = 0;
				for (int i = 1; i < length; i++)
				{
					stringItemInfo = item.TextData[i];
					if (!stringItemInfo.Equals(stringItemInfo2) && !editSyntaxPaint.EqualStyles(stringItemInfo2, stringItemInfo, true))
					{
						this.WriteLine(editSyntaxPaint, item.String, stringItemInfo2, num, i - 1);
						stringItemInfo2 = stringItemInfo;
						num = i;
					}
				}
				if (num < length)
				{
					this.WriteLine(editSyntaxPaint, item.String, stringItemInfo2, num, length - 1);
				}
			}
			else
			{
				StringItemInfo style = default(StringItemInfo);
				style.Data = 255;
				this.WriteLine(null, item.String, style, 0, length - 1);
			}
			this.EndLine();
		}
		public virtual void BeginWrite(TextWriter writer, object userData)
		{
			this.writer = writer;
			this.edit = (userData as CodeEditor);
			this.firstLine = true;
		}
		public virtual void EndWrite()
		{
		}
		public virtual bool Write()
		{
			return false;
		}
	}
}
